
import requests

def get_proxy():
    api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=okq31csvrpev75b9hfur&signature=1fxhdzeacflv8lzxg037p4n81r76fhxv&num=1&sep=1"

    # 获取API接口返回的代理IP
    proxy_ip = requests.get(api_url).text

    # 用户名密码认证(私密代理/独享代理)
    username = "d4667229674"
    password = "zrgaj7al"
    proxies = {
        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
    }

    return proxies


def get_proxies_poll(num=2):
    api_url = f"https://dps.kdlapi.com/api/getdps/?secret_id=okq31csvrpev75b9hfur&signature=1fxhdzeacflv8lzxg037p4n81r76fhxv&num={num}&format=json&sep=1"  # API接口

    proxies_poll = []
    # API接口返回的proxy_list
    proxy_list = requests.get(api_url).json().get('data').get('proxy_list')

    # 用户名密码认证(私密代理/独享代理)
    username = "d4667229674"
    password = "zrgaj7al"
    for proxy_ip in proxy_list:
        proxies = {
            "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
            "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
        }

        proxies_poll.append(proxies)

    return proxies_poll
